using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace EQEmulatorGUI
{
    public partial class frmTelnet : Form
    {
        private delegate void StatusUpdateMethod(UpdateStatusEventArgs args);
        public TcpClient client;
        public NetworkStream stream;
        public ASCIIEncoding asen = new ASCIIEncoding();
        public string _message = string.Empty;
        private AsyncCallback readCallback;
        private byte[] _buffer = new byte[2048];
        public string host = System.Configuration.ConfigurationManager.AppSettings["host"];
        public string uname = System.Configuration.ConfigurationManager.AppSettings["uname"];
        public string upass = System.Configuration.ConfigurationManager.AppSettings["upass"];
        public string dbhost = System.Configuration.ConfigurationManager.AppSettings["dbhost"];
        public string dbuser = System.Configuration.ConfigurationManager.AppSettings["dbuser"];
        public string dbpass = System.Configuration.ConfigurationManager.AppSettings["dbpass"];
        public string dbname = System.Configuration.ConfigurationManager.AppSettings["dbname"];
        public frmTelnet()
        {
            InitializeComponent();
            txtConsole.AppendText("Openning Connection to server. . .\n");
            txtConsole.AppendText("------------------------------------\n");
            client = new TcpClient(host, Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]));
            stream = client.GetStream();
            readCallback = new AsyncCallback(ReadFromStream);

            stream.BeginRead(_buffer, 0, _buffer.Length, readCallback, null);

            _message = "" + uname + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            _message = "" + upass + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            _message = "echo off\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);

            txtConsole.AppendText("Connected to server.\n");
            txtConsole.AppendText("------------------------------------\n");
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            _message = "acceptmessages on\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            
        }
        private void UpdateStatus(UpdateStatusEventArgs e)
        {
            txtConsole.AppendText(e.StatusMessage + "\n");
            
        }
        void ReadFromStream(IAsyncResult ar)
        {
            string returnMessage = string.Empty;

            try
            {
                returnMessage = Encoding.ASCII.GetString(_buffer, 0, stream.EndRead(ar));
            }
            catch (Exception)
            {
                return;
            }

            if (returnMessage.Trim().Length > 0)
            {
                if (txtConsole.InvokeRequired == true)
                    txtConsole.Invoke(new StatusUpdateMethod(UpdateStatus), new UpdateStatusEventArgs(returnMessage));
                    txtConsole.AppendText("------------------------------------\n");
            }
                else{
                    UpdateStatus(new UpdateStatusEventArgs(returnMessage));
            }

            _buffer = new byte[2048];
            stream.BeginRead(_buffer, 0, _buffer.Length, readCallback, null);
        }
        private void frmTelnet_Load(object sender, EventArgs e)
        {

        }

        private void btnBroadcast_Click(object sender, EventArgs e)
        {
            _message = "broadcast " + txtBroadcast.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtBroadcast.Text = "";
        }

        private void btnOOC_Click(object sender, EventArgs e)
        {
            _message = "ooc " + txtOOC.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtOOC.Text = "";
        }

        private void btnGMSay_Click(object sender, EventArgs e)
        {
            _message = "gmsay " + txtGmSay.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtGmSay.Text = "";
        }

        private void btnTell_Click(object sender, EventArgs e)
        {
            _message = "tell " + txtTellName.Text + " " + txtTellMsg.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtTellName.Text = "";
            txtTellMsg.Text = "";
        }

        private void btnZoneShutdown_Click(object sender, EventArgs e)
        {
            _message = "zoneshutdown " + txtZoneShutdown.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtZoneShutdown.Text = "";
        }

        private void btnBootZone_Click(object sender, EventArgs e)
        {
            _message = "zonebootup " + txtBootZoneID.Text + " " + txtBootZoneName.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtBootZoneID.Text = "";
            txtBootZoneName.Text = "";
        }

        private void btnFlag_Click(object sender, EventArgs e)
        {
            _message = "flag " + txtFlagStatus.Text + " " + txtFlagAccount.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtFlagStatus.Text = "";
            txtFlagAccount.Text = "";
        }

        private void btnSetPass_Click(object sender, EventArgs e)
        {
            _message = "setpass " + txtPassAccount.Text + " " + txtPassPassword.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtPassAccount.Text = "";
            txtPassPassword.Text = "";
        }

        private void btnKick_Click(object sender, EventArgs e)
        {
            _message = "kick " + txtKickChar.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtKickChar.Text = "";
        }

        private void btnIP_Click(object sender, EventArgs e)
        {
            _message = "IPLookup " + txtIPName.Text + "\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
            txtIPName.Text = "";
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            _message = "lock\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            _message = "unlock\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }

        private void btnWho_Click(object sender, EventArgs e)
        {
            
            _message = "who\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);

            txtConsole.AppendText("Who Command executed:\n");
        }

        private void btnUptime_Click(object sender, EventArgs e)
        {
            
            _message = "uptime\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }

        private void btnWhoamI_Click(object sender, EventArgs e)
        {
            _message = "whoami\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }

        private void btnVersion_Click(object sender, EventArgs e)
        {            
            _message = "version\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }

        private void frmTelnet_FormClosed(object sender, FormClosedEventArgs e)
        {
            stream.Close();
            client.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _message = "acceptmessages on\n";
            stream.Write(asen.GetBytes(_message), 0, _message.Length);
        }


    }//CLASS

        public class UpdateStatusEventArgs : System.EventArgs
    {
        #region "Private Member Variables"
        private readonly string sStatusMessage;
        #endregion

        #region "Public Properties"
        public string StatusMessage
        {
            get { return sStatusMessage; }
        }
        #endregion

        #region "Constructors"
        public UpdateStatusEventArgs(string statusMessage)
        {
            sStatusMessage = statusMessage;
        }
        #endregion
    }
}
